﻿/***************************************************************************
 *		ElGee Google Chart API Helper
 *		Copyright(C) 2011 by Laurent Geffroy lgeffroy@gmail.com
 * 
 *		This program is free software; you can redistribute it and/or 
 *		modify it under the terms of the GNU General Public License as
 *		published by the Free Software Foundation; either version 2 of 
 *		the License, or (at your option) any later version.
 *		
 *		This program is distributed WITHOUT ANY WARRANTY; without even the
 *		implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *		See the GNU General Public License for more details.
 *	
 *      For particular licensing, please contact me at lgeffroy@gmail.com
 *****************************************************************************/
using System;
using System.Globalization;
using System.Text;

namespace ElGee.Google.Charts
{
    /// <summary>
    /// Area Class
    /// </summary>
    public class Area : ChartBase
    {

        /// <summary>
        /// Gets the top.
        /// How far to draw the chart from the top border.
        /// </summary>
        /// <value>chartArea.top</value>
        public String Top { get; private set; }

        /// <summary>
        /// Gets the width.
        /// Chart area width.
        /// </summary>
        /// <value>chartArea.width</value>
        public String Width { get; set; }

        /// <summary>
        /// Gets the height.
        /// Chart area height.
        /// </summary>
        /// <value>chartArea.height</value>
        public String Height { get; set; }

        /// <summary>
        /// Defines the top.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="isPercentage">if set to <c>true</c> [is percentage].</param>
        public void DefineTop(UInt16 value, bool isPercentage)
        {
            if ((isPercentage) && (value > 100))
                throw new ArgumentException("Percentage cannot be greater than 100", "value");

            Top = value.ToString(CultureInfo.InvariantCulture);
            if (isPercentage)
                Top += "%";
        }

        /// <summary>
        /// Defines the width.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="isPercentage">if set to <c>true</c> [is percentage].</param>
        public void DefineWidth(UInt16 value, bool isPercentage)
        {
            if ((isPercentage) && (value > 100))
                throw new ArgumentException("Percentage cannot be greater than 100", "value");

            Width = value.ToString(CultureInfo.InvariantCulture);
            if (isPercentage)
                Width += "%";
        }

        /// <summary>
        /// Defines the height.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="isPercentage">if set to <c>true</c> [is percentage].</param>
        public void DefineHeight(UInt16 value, bool isPercentage)
        {
            if ((isPercentage) && (value > 100))
                throw new ArgumentException("Percentage cannot be greater than 100", "value");

            Height = value.ToString(CultureInfo.InvariantCulture);
            if (isPercentage)
                Height += "%";
        }



        /// <summary>
        /// Gets a value indicating whether this instance is empty.
        /// </summary>
        /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
        public bool IsEmpty
        {
            get
            {
                if ((!String.IsNullOrWhiteSpace(Top)) && (!String.IsNullOrWhiteSpace(Width)) && (!String.IsNullOrWhiteSpace(Height)))
                    return false;
                return true;
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (IsEmpty)
                return String.Empty;

            StringBuilder sb = new StringBuilder();
            sb.Append("chartArea: {");

            if (!String.IsNullOrWhiteSpace(Top))
            {
                if (Top.Contains("%"))
                    sb.Append("top: '").Append(SecureString(Top)).Append("%',");
                else
                    sb.Append("top: ").Append(SecureString(Top)).Append(",");
            }

            if (!String.IsNullOrWhiteSpace(Width))
            {
                if (Width.Contains("%"))
                    sb.Append("width: '").Append(SecureString(Width)).Append("%',");
                else
                    sb.Append("width: ").Append(SecureString(Width)).Append(",");
            }

            if (!String.IsNullOrWhiteSpace(Height))
            {
                if (Height.Contains("%"))
                    sb.Append("height: '").Append(SecureString(Height)).Append("%',");
                else
                    sb.Append("height: ").Append(SecureString(Height)).Append(",");
            }

            // Removing the last comma
            sb.Remove(sb.Length - 1, 1);
            sb.Append("}");

            return sb.ToString();
        }
    }

}
